<template>
	<view>
		<j-cell-group>
			<!-- 摇头机 -->
			<j-cell v-if="ability.OtherFunction.SupportStatusLed" title="开启状态灯" :more="false">
				<view slot="tips">
					<switch :checked="ison" color="#FF0025" style="transform:scale(0.8)"
						@change="isonChange"></switch>
				</view>
			</j-cell>
			<j-cell v-if="ability.OtherFunction.SupportCloseVoiceTip" title="提示音" :more="false" >
				<view slot="tips">
					<switch :checked="PlayVoiceTip" color="#FF0025" style="transform:scale(0.8)"
						@change="PlayVoiceTipChange"></switch>
				</view>
			</j-cell>
			<j-cell title="设备名称" :tips="deviceName" @jftap="onBindtapUpdate"></j-cell>
			<j-cell v-if="pid === constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT || pid === constant.DEVICE_TYPE.EE_DEV_NORMAL_MONITOR" title="图像左右反转" :more="false">
				<view slot="tips">
					<switch :checked="PictureMirror" color="#FF0025" style="transform:scale(0.8)"
						@change="leftRightSwitchChange"></switch>
				</view>
			</j-cell>
			<j-cell v-if="pid === constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT || pid === constant.DEVICE_TYPE.EE_DEV_NORMAL_MONITOR" title="图像上下反转" :more="false">
				<view slot="tips">
					<switch :checked="PictureFlip" color="#FF0025" style="transform:scale(0.8)"
						@change="upDownSwitchChange"></switch>
				</view>
			</j-cell>
			<j-cell title="设备语言" :tips="language"
				:to="`/jvss/pagesDevice/device/device-live/setting/selectItem?type=1&token=${token}`"></j-cell>
			<!-- <j-cell-card title="摇头速度" :show="false" :is-touch="false">
				<template v-for="(item, index) in levels">
					<j-cell :key="index" :title="item.name" :more="false" @jftap="bindchangeLevels(item)">
						<view slot="tips" v-if="headSpeed === item.value">
							<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
							</uni-icons>
						</view>
					</j-cell>
				</template>
			</j-cell-card> -->
			<j-cell v-if="Number(pid) === parseInt(constant.DEVICE_TYPE.EE_DEV_LOW_POWER, 16)" title="设备自动休眠时间" subTitle="时间越长,设备的续航时间就会越短" :tips="sleepTime"
				:to="`/jvss/pagesDevice/device/device-live/setting/selectItem?type=2&token=${token}`"></j-cell>
			<!-- 摇头机 -->
			<j-cell v-if="isSupportPTZDevice" title="云台左右翻转" :more="false">
				<view slot="tips">
					<switch :checked="videoleft" color="#FF0025" style="transform:scale(0.8)"
						@change="videoleftRightSwitchChange"></switch>
				</view>
			</j-cell>
			<j-cell v-if="isSupportPTZDevice" title="云台上下反转" :more="false">
				<view slot="tips">
					<switch :checked="videoup" color="#FF0025" style="transform:scale(0.8)"
						@change="videoupDownSwitchChange"></switch>
				</view>
			</j-cell>
			<j-cell v-if="ability.OtherFunction.SupportDNChangeByImage" title="日夜切换灵敏度" :more="false">
				<view slot="tips" class="laba">
					<slider max="10" style="width: 100%;" :value="AeSensitivity" @change="AeSensitivityChange" step="1"
						show-value />
				</view>
			</j-cell>
			<j-cell v-if="ability.OtherFunction.SupportSetVolume" title="喇叭音量" :more="false" >
				<view slot="tips" class="laba">
					<slider style="width: 100%;" :value="volume" @change="sliderChange" step="1" show-value />
				</view>
			</j-cell>
		</j-cell-group>

		<uni-popup ref="popup" @change="change" @maskClick="maskClick" class="popupName">
			<form @submit="submit">
				<view class="form">
					<view class="form-item">
						<input :focus="focus" placeholder="请输入设备名称" name="deviceName" v-model="modifyName"
							class="form-control" style="background: #f6f6f6" @confirm="submit" />
					</view>
					<view class="form-item">
						<button class="block radius-btn" type="primary" form-type="submit">确认</button>
					</view>
				</view>
			</form>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				deviceNo: '',
				deviceName: '',
                modifyName: '',
				isSupportPTZDevice: false, //是否是摇头机
				// 弹出窗口-表单
				focus: false,
				language: '',
				sleepTime: '',
				percent: 20,
				cameraQuery: {},
				PictureFlip: false, //'0x00000000',// 图片上下翻转
				PictureMirror: false, //'0x00000000',// 图片左右翻转
				videoleft: uni.getStorageSync('turn_video_left') ? uni.getStorageSync('turn_video_left').status :
				false, // 云台上下翻转
				videoup: uni.getStorageSync('turn_video_up') ? uni.getStorageSync('turn_video_up').status :
				false, // 云台上下翻转
				volume: 0,
				AeSensitivity: 0,
				volumeQuery: {}, //喇叭音量
				lanQuery: {}, //设备语言
				AeSensitivityQuery: {},
				PlayVoiceTip: false, //提示音
				PlayVoiceTipQuery: {},
				ison:false,//状态灯
				levels: [{
						value: 1,
						name: '慢'
					},
					{
						value: 4,
						name: '中'
					},
					{
						value: 8,
						name: '快'
					}
				],
				headSpeed: uni.getStorageSync('turn_around_speed') ? uni.getStorageSync('turn_around_speed').speed : 1,
				ability:'',
				pid:"",
				constant:getApp().globalData.constant
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onShow() {
			this.getCameraParam()
			this.getDeviceLanguage()
		},
		onLoad: function(options) {
			console.log('=== basic:onLoad:', options);
			// let device = JSON.parse(options.device)
			this.ability = JSON.parse(options.ability)
			console.log(this.ability)
			this.deviceNo = options.deviceNo
			this.deviceName = options.deviceName
            this.modifyName = options.deviceName
			this.token = options.token
			this.pid = options.pid
			this.isSupportPTZDevice = JSON.parse(options.isSupportPTZDevice) //是否是摇头机
			// this.getCameraParam()
			// this.getDeviceLanguage()
			if(Number(this.pid) === parseInt(this.constant.DEVICE_TYPE.EE_DEV_LOW_POWER, 16)){
				this.getSleepTime()
			}
			this.getFVideoVolumeParam()
			this.getAeSensitivity()
			this.getFbExtraStateCtrl()
			let str = '0x1'
			console.log('sssdggdgsdg', parseInt(str));
		},
		methods: {
			//获取Camera参数
			getCameraParam() {
				let data = {
					Name: 'Camera.Param'
				} //{Name:'Camera'}
				this.getConfig(data)
			},
			//获取喇叭音量fVideo.Volume参数
			getFVideoVolumeParam() {
				let data = {
					Name: 'fVideo.Volume'
				}
				this.getConfig(data)
			},
			//获取设备语言
			getDeviceLanguage() {
				let data = {
					Name: 'General.Location'
				}
				this.getConfig(data)
			},
			//获取当前睡眠时间
			getSleepTime() {
				let data = {
					Name: 'System.ManageShutDown'
				}
				this.getConfig(data)
			},
			// 灵敏度
			getAeSensitivity() {
				let data = {
					Name: 'Camera'
				}
				this.getConfig(data)
			},
			// 设备提示音
			getFbExtraStateCtrl() {
				let data = {
					Name: 'FbExtraStateCtrl'
				}
				this.getConfig(data)
			},
			// 操作提示音
			PlayVoiceTipChange() {
				console.log(this.PlayVoiceTipQuery.FbExtraStateCtrl.PlayVoiceTip)
				if (!this.PlayVoiceTip) {
					this.PlayVoiceTipQuery.FbExtraStateCtrl.PlayVoiceTip = 1
				} else {
					this.PlayVoiceTipQuery.FbExtraStateCtrl.PlayVoiceTip = 0
				}
				this.setConfig(this.PlayVoiceTipQuery)
			},
			isonChange(){
				if (!this.ison) {
					this.PlayVoiceTipQuery.FbExtraStateCtrl.ison = 1
				} else {
					this.PlayVoiceTipQuery.FbExtraStateCtrl.ison = 0
				}
				this.setConfig(this.PlayVoiceTipQuery)
			},
			// //左右翻转
			leftRightSwitchChange() {
				if (!this.PictureMirror) {
					this.cameraQuery["Camera.Param"][0].PictureMirror = '0x1'
				} else {
					this.cameraQuery["Camera.Param"][0].PictureMirror = '0x0'
				}
				console.log('左右翻转==', this.PictureMirror);
				this.setConfig(this.cameraQuery)
			},
			//上下翻转
			upDownSwitchChange() {
				console.log('上下翻转==', this.PictureFlip);
				if (!this.PictureFlip) {
					this.cameraQuery["Camera.Param"][0].PictureFlip = '0x1'
				} else {
					this.cameraQuery["Camera.Param"][0].PictureFlip = '0x0'
				}
				this.setConfig(this.cameraQuery)
			},
			// 摇头速度
			bindchangeLevels(ev) {
				console.log(ev);
				this.headSpeed = ev.value
				let obj = {
					speed: ev.value,
					deviceNo: this.deviceNo
				}
				uni.setStorageSync('turn_around_speed', obj)
			},
			videoupDownSwitchChange(ev) {
				this.videoup = ev.detail.value
				let obj = {
					status: ev.detail.value,
					deviceNo: this.deviceNo
				}
				uni.setStorageSync('turn_video_up', obj)
			},
			videoleftRightSwitchChange(ev) {
				this.videoleft = ev.detail.value
				let obj = {
					status: ev.detail.value,
					deviceNo: this.deviceNo
				}
				uni.setStorageSync('turn_video_left', obj)
			},
			// 设置喇叭音量大小
			sliderChange(e) {
				this.volume = e.detail.value
				this.volumeQuery['fVideo.Volume'][0].LeftVolume = this.volume
				this.volumeQuery['fVideo.Volume'][0].RightVolume = this.volume
				setTimeout(() => {
					this.setConfig(this.volumeQuery)
				}, 1000)
				console.log('slider', e);
			},
			// 设置日夜切换灵敏度
			AeSensitivityChange(e) {
				this.AeSensitivity = e.detail.value
				this.AeSensitivityQuery['Camera'].Param[0].AeSensitivity = this.AeSensitivity
				setTimeout(() => {
					this.setConfig(this.AeSensitivityQuery)
				}, 1000)
			},
			// 获取参数
			getConfig(data) {
				let token = this.token;
				getApp().globalData.api.getconfig(data, token, (result) => {
					console.log('获取参数：', result);
					//翻转
					if (result.Name == 'Camera.Param') {
						this.cameraQuery = result
						this.PictureFlip = parseInt(result['Camera.Param'][0].PictureFlip) == 1 ? true : false
						this.PictureMirror = parseInt(result['Camera.Param'][0].PictureMirror) == 1 ? true : false
						console.log('上下翻转==', this.PictureFlip);
						console.log(' 左右翻转==', this.PictureMirror);
					}
					// 灵敏度
					if (result.Name == 'Camera') {
						console.log(result)
						this.AeSensitivityQuery = result
						this.AeSensitivity = result.Camera.Param[0].AeSensitivity
					}
					// 提示音
					if (result.Name == 'FbExtraStateCtrl') {
						console.log(result, '提示音')
						this.PlayVoiceTipQuery = result
						this.PlayVoiceTip = result.FbExtraStateCtrl.PlayVoiceTip == 1 ? true : false
						this.ison = result.FbExtraStateCtrl.ison == 1 ? true : false
					}
					//音量
					if (result.Name == 'fVideo.Volume') {
						this.volumeQuery = result
						this.volume = result['fVideo.Volume'][0].LeftVolume
					}
					//设备语言
					if (result.Name == 'General.Location') {
						this.lanQuery = result
						this.language = result['General.Location'].Language
					}
					//睡眠时间
					if (result.Name == 'System.ManageShutDown') {
						let time = result['System.ManageShutDown'].ShutDownMode
						if (time == -2) {
							this.sleepTime = '永不休眠'
						} else {
							this.sleepTime = time + '秒'
						}
						console.log("this.sleepTime: ", this.sleepTime);
						console.log("this.sleepTime22: ", time == -2);
					}
				})
			},
			//设置参数
			setConfig(data) {
				let token = this.token;
				getApp().globalData.api.setconfig(data, token, (result) => {
					console.log('设置参数：', result);
				})
			},
			//更新设备名称
			onBindtapUpdate() {
				this.$refs.popup.open()
				setTimeout(() => {
					this.focus = true
					this.deviceName = this.deviceName
				}, 350);
			},
			submit(ev) {
				if (this.deviceName.replace(/\s/g, '') === '') {
					uni.showToast({
						title: '请输入设备名称'
					});
					return;
				}

				let data = {
					deviceNo: this.deviceNo,
					deviceName: this.modifyName
				};
				getApp().globalData.api.updateDevice(data, (result) => {
					this.deviceName = this.modifyName;
					this.$refs.popup.close()
					this.focus = false
					let deviceList = uni.getStorageSync('deviceList');

					for (let i = 0; i < deviceList.length; i++) {
						const item = deviceList[i];

						if (item.deviceNo === this.deviceNo) {
							item.deviceName = this.deviceName;
							break;
						}
					}

					uni.setStorageSync('deviceList', deviceList);
				});
			},

			change(ev) {
				console.log('change:', ev);
			},

			maskClick(ev) {
				console.log('maskClick:', ev);
				this.focus = false
                this.modifyName = this.deviceName
			}
		}
	};
</script>
<style lang="scss">
    .popupName {
        /deep/ .uni-popup__wrapper {
            //top: -100rpx;
            top: -100rpx;
        }
    }
	.form {
		background: white;
		padding: 60rpx;
		border-radius: 10rpx;
	}

	.form .form-item {
		margin-top: 20rpx;
	}

	.form .form-title {
		text-align: center;
		margin-bottom: 40rpx;
		font-weight: bold;
	}

	.form .form-item .form-control {
		background: #efefef;
	}

	.laba {
		width: calc(50vw - 50rpx);
		display: flex;
		justify-content: end;
		margin-right: 20rpx;
	}
</style>
